import util from '../../../utils/utils';
import {
  HomeHttp
} from '../../../api/home.js'
import {
  UserHttp
} from '../../../api/user.js'

import Dialog from '../../../dist/dialog/dialog';
const userHttp = new UserHttp()

const homeModel = new HomeHttp();

Page({
  data: {
    pageUrl: util.pageUrl,
    // recommendList: [],
    recommendPage: 1,
    goodsInfo: {},
    isShowPopup: false,
    freesheettList: [], //正在参与免单的人员
    orderInfo: {},
    tips: {},
    orderId: 0,
    peopleNum: 0,
    shareImageUrl: '' //免单分享图
  },
  initPage: function(options) {
    if (options.hasOwnProperty('id')) {
      this.setData({
        orderId: options.id
      })
      this.httpFreesheetImg();
      this.httpGetOrderDetailr();
      // this.getNewFreesheet();
    }
    // this.httpGetGoodsRecommend(false);


  },
  onViewOrderDetail() {
    wx.navigateTo({
      url: `/pages/order/ownOrder/ownOrder?id=${this.data.orderId}`
    })
  },
  //获取我要免单分享图
  httpFreesheetImg() {
    let that = this;
    let order_id = this.data.orderId;
    if (!order_id) {
      console.log('订单ID缺失');
      return
    }
    homeModel.FreesheetImg({
      ordernum: order_id
    }).then((res) => {
      let img = res;
      that.setData({
        shareImageUrl: img
      })
    }).catch((err) => {
      console.log(`err`, err)
    })
  },
  //正在参与的人员
  getNewFreesheet() {
    homeModel.newFreesheett({}).then((res) => {
      console.log(res)
      this.setData({
        freesheettList: this.formatData(res.order_info),
        peopleNum: res.total
      })
    })
  },

  formatData(res) {
    let i = 0;
    for (i; i < res.length; i++) {
      res[i]['longTimeAgo'] = this.timeago(res[i]['updatetime'] * 1000)
    }
    return res
  },

  //转换为多久以前
  timeago(dateTimeStamp) {
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var now = new Date().getTime();
    var diffValue = now - dateTimeStamp;
    if (diffValue < 0) {
      return;
    }
    var minC = diffValue / minute;
    var hourC = diffValue / hour;
    var dayC = diffValue / day;
    var result = '';
    if (dayC >= 1 && dayC <= 3) {
      result = "在" + parseInt(dayC) + "天前";
    } else if (hourC >= 1 && hourC <= 24) {
      result = "在" + parseInt(hourC) + "小时前";
    } else if (minC >= 1 && minC <= 60) {
      result = "在" + parseInt(minC) + "分钟前";
    } else if (minC < 1) {
      result = "刚刚";
    }
    return result;
  },

  //价格最高的商品
  maxPriceGoods(res) {
    let i = 0;
    let goods = res[0]
    let goodsNum = 0
    for (i; i < res.length; i++) {
      //计算数量
      goodsNum = goodsNum + res[i]['goodsNum']
      //判断最高价格
      if (res[i]['realPrice'] > goods['realPrice']) {
        goods = res[i]
      }
    }
    goods['goodsNum'] = goodsNum
    return goods
  },



  // 获取订单信息
  httpGetOrderDetailr() {
    return userHttp.getOrderDetailr({
      orderNumber: this.data.orderId
    }).then((res) => {
      this.setData({
        // goodsInfo: this.maxPriceGoods(res.goodsInfo),
        goodsInfo: res.goodsInfo,
        orderInfo: res
      })
      console.log(res)
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    }).catch(err => {
      util.msg("网络错误,请检查后重试!")
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    })
  },
  toTeamPage() {
    let userInfo = wx.getStorageSync("_uInfo");
    if (userInfo && userInfo.isInviter == 1) {
      Dialog.alert({
        theme: 'round-button',
        message: '您已经是团长了，现在就去分享商品!',
        showCancelButton: true,
        confirmButtonText: "去分享",
      }).then((res) => {
        wx.switchTab({
          url: '/pages/home/index/index'
        })
      }).catch((err) => {
        console.log(err)
      });
    } else {
      util.toPage("/pages/me/setting/team/index");
    }
  },
  onShareAppMessage() {
    let shareImageUrl = this.data.shareImageUrl;
    let order_id = this.data.orderId;
    let url = '/pages/order/freeSheet/index?id=' + order_id;
    this.httpJlKnifeOrder();
    if (!shareImageUrl || !order_id) {
      // util.msg('分享图不见了！');
      console.log('分享参数缺失')
      console.log('imageUrl:' + shareImageUrl);
      console.log('order_id:' + order_id);
      return
    }

    //我要免单
    return {
      title: this.data.userInfo.nickname + "@您，我在巷友申请免单，快来帮帮我",
      path: url,
      imageUrl: shareImageUrl
    }
  },

  httpJlKnifeOrder() {
    userHttp.jlKnifeOrder({
      ordernum: this.data.orderId
    }).then((res) => {

    })
  },

  //显示规则
  onShowPopup() {
    this.setData({
      isShowPopup: !this.data.isShowPopup
    })
  }
});